<template>
  <div class="activity">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card class="box-card">
          <div class="boxcard-head">
            <el-form :inline="true" class="demo-form-inline">
              <el-form-item>
                <el-button type="primary" icon="el-icon-plus" @click="dialogAdd()">新增</el-button>
              </el-form-item>
            </el-form>
          </div>
          <div class="boxcard-body">
            <el-table
              :data="list"
              style="width: 100%"
              row-key="id"
              :header-cell-style="{ 'background-color': '#EEEEEE' }"
            >
              <el-table-column prop="imageUrl" label="活动图片">
                <template slot-scope="scope">
                  <el-image :src="scope.row.imageUrl" style="width:60px;height:60px" :preview-src-list="[scope.row.imageUrl]"  />
                </template>
              </el-table-column>
              <el-table-column prop="beginDate" label="活动有效时间" show-overflow-tooltip>
                <template slot-scope="scope">{{ scope.row.beginDate + '-' + scope.row.endDate }}</template>
              </el-table-column>
              <el-table-column prop="status" label="状态" show-overflow-tooltip>
                <template slot-scope="scope">
                  <el-tag>{{ getStatus(scope.row.status) }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="sort" label="排序" show-overflow-tooltip></el-table-column>

              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button @click="dialogField(scope.row)" type="text" size="small">编辑</el-button>
                  <el-button v-if="scope.row.status!=2" @click="publish(scope.row)" type="text" size="small">发布</el-button>
                  <el-button v-if="scope.row.status!=4" @click="deleteById(scope.row)" type="text" size="small">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div class="pagination">
              <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="pageChange"
                :total="total"
                :current-page.sync="pageNum"
                :page-sizes="[10, 20]"
                :page-size="10"
                layout="total, sizes, prev, pager, next"
              ></el-pagination>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 弹窗 -->
    <div>
      <el-dialog
        :title="editName"
        :visible.sync="dialogFieldBox"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :show-close="false"
      >
        <el-form
          :model="preferentialActivity"
          :rules="rules"
          label-position="right"
          label-width="100px"
          ref="form"
        >
          <el-form-item label="活动图片" prop="imageUrl">
            <singleImgUpload :url.sync="preferentialActivity.imageUrl" dir="preferentialActivity"></singleImgUpload>
          </el-form-item>
          <el-form-item label="活动有效时间" label-width="70" prop="date">
            <el-date-picker
              v-model="preferentialActivity.date"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input v-model="preferentialActivity.sort" type="number" />
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelClick">取 消</el-button>
          <el-button type="primary" @click="confirmClick">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { get, postJson, postXform } from "@/utils/request.js";
import singleImgUpload from "../../components/upload/SingleImgUpload.vue";

export default {
  components: { singleImgUpload },
  data() {
    return {
      // 分页参数
      pageNum: 1, // 当前页数
      pageSize: 10, // 页大小
      total: 0, // 数据总条数
      list: [], // 数据列表

      // 弹窗相关属性
      editName: "编辑",
      dialogFieldBox: false,
      // 弹窗数据
      preferentialActivity: {
        id: null,
        beginDate: null,
        endDate: null,
        sort: null,
        imageUrl: null,
        date: []
      },
      // 表单校验
      rules: {},
      // 数据字典
      statuss: [],
    };
  },
  watch: {},
  created() {
    this.ininDict();
    this.getList(this.pageNum, this.pageSize);
  },
  methods: {
    async ininDict() {
      await get("preferentialActivity/status").then(res => {
        this.statuss = res.data.data;
      });
    },
    getStatus(status) {
      if (status) {
        for (let i = 0; i < this.statuss.length; i++) {
          if (this.statuss[i].key === status) {
            return this.statuss[i].label;
          }
        }
      }
      return "";
    },
    // 分页相关方法
    // 页大小改变时
    handleSizeChange(val) {
      this.pageSize = val;
    },
    // 页码改变时
    pageChange(e) {
      // 获取某页数据
      this.pageNum = e;
      this.getList();
    },
    // 获取列表数据
    getList(num, size) {
      get("/preferentialActivity/page", {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }).then(res => {
        this.list = res.data.data.list;
        // 自定义下标
        this.list.forEach((e, idx) => {
          e.idx = (idx + 1) * this.pageNum;
        });
        this.total = Number.parseInt(res.data.data.total);
        this.pageNum = res.data.data.pageNum;
      });
    },
    publish(row) {
      postXform("/preferentialActivity/publish", { id: row.id }).then(res => {
        this.$message({ message: "操作成功", type: "success" });
        this.getList();
      });
    },
    deleteById(row) {
      this.$confirm("是否删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          postXform("/preferentialActivity/delete", { id: row.id }).then(
            res => {
              this.$message({ message: "删除成功", type: "success" });
              this.getList();
            }
          );
        })
        .catch(() => {});
    },
    resetAdvertisement() {
      return {
        id: null,
        beginDate: null,
        endDate: null,
        sort: 1,
        imageUrl: null,
        date: []
      };
    },
    dialogAdd() {
      this.editName = "新增";
      this.dialogFieldBox = true;
      this.preferentialActivity = this.resetAdvertisement();
    },
    // 编辑按钮打开编辑字段弹窗
    dialogField(e) {
      this.editName = "编辑";
      this.dialogFieldBox = true;
      this.preferentialActivity = {
        id: e.id,
        beginDate: e.beginDate,
        endDate: e.endDate,
        sort: e.sort,
        imageUrl: e.imageUrl,
        date: []
      };
      if (e.beginDate) {
        this.preferentialActivity.date.push(e.beginDate);
      }
      if (e.endDate) {
        this.preferentialActivity.date.push(e.endDate);
      }
    },
    // 确认保存
    confirmClick() {
      const vm = this;
      let param = { ...this.preferentialActivity };
      if (param.date.length > 0) {
        param.beginDate = param.date[0];
        param.endDate = param.date[1];
        if (param.date[0].length <= 10) {
          param.beginDate = param.date[0] + " 00:00:00";
          param.endDate = param.date[1] + " 23:59:59";
        }
      }
      this.$refs.form.validate(valid => {
        if (valid) {
          let reqUel = "/preferentialActivity/add";
          if (this.preferentialActivity.id) {
            reqUel = "/preferentialActivity/update";
          }
          postXform(reqUel, param).then(res => {
            this.$message({ message: "操作成功", type: "success" });
            this.dialogFieldBox = false;
            this.getList();
            this.preferentialActivity = this.resetAdvertisement();
          });
        } else {
          return false;
        }
      });
    },
    cancelClick() {
      this.dialogFieldBox = false;
      this.$refs.form.clearValidate();
      this.preferentialActivity = this.resetAdvertisement();
    },
  }
};
</script>

<style lang="less"></style>
